package com.wzl.ma;

import java.io.IOException;

import android.app.Activity;
import android.content.ContentValues;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AudioApp extends Activity {

	Button StartPlay;
	Button PausePlay;
	Button Resume;
	Button StopPlay;
	Button StartRec;
	Button PauseRec;
	Button StopRec;
	MediaRecorder recorder = new MediaRecorder();
	MediaPlayer mp = new MediaPlayer();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		StartPlay = (Button) findViewById(R.id.btn_StartPlay);
		PausePlay = (Button) findViewById(R.id.btn_PausePlay);
		Resume = (Button) findViewById(R.id.btn_Resume);
		StopPlay = (Button) findViewById(R.id.btn_StopPlay);
		StartRec = (Button) findViewById(R.id.btn_StartRec);
		PauseRec = (Button) findViewById(R.id.btn_PauseRec);
		StopRec = (Button) findViewById(R.id.btn_StopRec);

		StartPlay.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				// Context context;
				// MediaPlayer mp = MediaPlayer.create(context,
				// R.raw.sound_file_1);
				// mp.start();

				// MediaPlayer mp = new MediaPlayer();
				try {
					mp.setDataSource("/sdcard/RecorderTest.3gpp");
				} catch (IllegalArgumentException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IllegalStateException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				try {
					mp.prepare();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				mp.start();

			}
		});

		StopRec.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				mp.stop();
				mp.reset();
				mp.release();
			}
		});

		StartRec.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				// MediaRecorder recorder = new MediaRecorder();
				ContentValues values = new ContentValues(3);

				values.put(MediaStore.MediaColumns.TITLE, "MySound");
				values.put(MediaStore.MediaColumns.DATE_ADDED, System
						.currentTimeMillis());
				// values.put(MediaStore.MediaColumns.MIME_TYPE,
				// recorder.getMimeContentType());
				// values.put(MediaStore.MediaColumns.MIME_TYPE,((Object)
				// recorder).getMimeContentType());

				// ContentResolver contentResolver = new ContentResolver();
				// Uri base = MediaStore.Audio.;
				// Uri newUri = contentResolver.insert(base, values);

				// if (newUri == null){
				// need to handle exception here - we were not able to create a
				// new
				// content entry

				// }
				String path = "/sdcard/RecorderTest.3gpp";
				// could use setPreviewDisplay() to display a preview to
				// suitable view here

				recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
				recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
				recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
				recorder.setOutputFile(path);
				try {
					recorder.prepare();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				recorder.start();

			}
		});

		StopRec.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				recorder.stop();
				recorder.reset();
				recorder.release();
			}
		});
	}
}